package com.web.biz.recruit;
import java.util.List;
import java.util.Map;

import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.kernel.utils.SwordSequenceUtils;
import com.css.sword.kernel.utils.SwordServiceUtils;
import com.css.sword.platform.web.adapter.newsword.pagequery.IPageQueryDAO;
import com.css.sword.platform.web.adapter.newsword.utils.PageQueryUtils;
import com.css.sword.platform.web.comm.WebRootUtil;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;
import com.web.common.dataDictionary.DE.DE_CATEGORY_ID;
import com.web.common.dataDictionary.DE.DE_CATEGORY_OPPTIME;
import com.web.common.dataDictionary.DE.DE_CATEGORY_SFNEW;
import com.web.common.dataDictionary.DE.DE_C_BRAND_NEWNUM;
import com.web.common.dataDictionary.DE.DE_ID;
import com.web.common.dataDictionary.DE.DE_WEBDOMAIN;
import com.web.common.dataDictionary.DE.DE_WEBPATH;
import com.web.common.dataDictionary.DE.DE_WEBUSERNAME;
import com.web.common.dataDictionary.DE.DE_ZP_FBSJ;
import com.web.common.dataDictionary.DE.DE_ZP_LOGO;
import com.web.common.dataDictionary.DE.DE_ZP_OPPTIME;
import com.web.common.dataDictionary.DE.DE_ZP_SHOPNAME;
import com.web.common.dataDictionary.DE.DE_ZP_SHSJ;
import com.web.common.dataDictionary.DE.DE_ZP_SHZT;
import com.web.common.dataDictionary.PO.TCategoryPO;
import com.web.common.dataDictionary.PO.TCategoryShopPO;
import com.web.common.dataDictionary.PO.TabRecruitPO;
import com.web.common.utils.CacheUtil;
import com.web.common.utils.DateFormatUtil;
import com.web.common.utils.LoginInfoUtil;
import com.web.common.utils.StringUtil;

@ServiceContainer
public class RecruitService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(RecruitService.class);
	
	public RecruitService() throws Exception {
		super();
	}


	/**
	 * 初始化recruit列表
	 */
	@SuppressWarnings("rawtypes")
	@Service(serviceName = "RecruitService_initList")
	public IResData initList(IReqData req) throws Exception {

		log.debug("开始进入 RecruitService_initList 方法。");

		IPageQueryDAO pdao=PageQueryUtils.getPageQueryDAO();
		IResData res = new SwordRes();
		SQLParam param = new SQLParam();
		param.addParam(LoginInfoUtil.getLoginUserName());
		List list = pdao.pageQueryByKey("grid_recruit", "RecruitService_queryListByYhm", param);
		
		res.addTable("grid_recruit", list);
		res.addPage("pages/biz/recruit/recruitList.jsp");
		return res;

	}

	 		
	/**
	 * 初始化recruit表单
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Service(serviceName = "RecruitService_initForm")
	public IResData initForm(IReqData req) throws Exception {

		log.debug("开始进入 RecruitService_initForm 方法。");

		//IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		IResData res = new SwordRes();
		String yhm=LoginInfoUtil.getLoginUserName();
		
		//往前台返回一些数据用来页面作判断开始
		//用户信息
		Map<String, Object> userMap = (Map<String, Object>) SwordServiceUtils.callLocalService("LoginRegisterService_queryByYhm", yhm);
		res.addJSTL("vipsyts", StringUtil.parse2String(userMap.get("vipsyts")));
		res.addJSTL("shzt", StringUtil.parse2String(userMap.get("shzt")));
		//店铺信息
		Map shopMap =(Map) SwordServiceUtils.callLocalService("LoginRegisterService_queryShopxxByYhm", yhm);
		String brandwebshzt = StringUtil.parse2String(shopMap.get("webshzt"));
		int newnum = (Integer)shopMap.get("cBrandNewnum");
		int totalwealth = (Integer) shopMap.get("cBrandTotalwealth");
		res.addJSTL("brandwebshzt", brandwebshzt);
		res.addJSTL("newnum", newnum);
		res.addJSTL("totalwealth", totalwealth);
		
		res.addPage("pages/biz/recruit/recruitForm.jsp");
		return res;

	}

	 		
	/**
	 * 打开查看recruit只读信息
	 */
	@Service(serviceName = "RecruitService_openShow")
	public IResData openShow(IReqData req) throws Exception {

		log.debug("开始进入 RecruitService_openShow 方法。");

		IResData res = new SwordRes();
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		SQLParam param = new SQLParam();
		param.addParam(StringUtil.parse2String(req.getAttr("id")));
		Map<String, Object> map = dao.queryMapByKey("RecruitService_queryById", param);
		
		res.addForm("form_recruitdetail", map);
		res.addPage("pages/biz/recruit/recruitDetailForm.jsp");
		return res;
	}

	 		
	/**
	 * tjWeb
	 */
	@SuppressWarnings("rawtypes")
	@Service(serviceName = "RecruitService_tjWeb")
	public IResData tjWeb(IReqData req) throws Exception {

		log.debug("开始进入 RecruitService_tjWeb 方法。");

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		dao.getTransactionService().setAutoCommit(false);
		IResData res = new SwordRes();
		res = (IResData) SwordServiceUtils.callLocalService("FreeMarkerService_genHtmlAndGetPath", req);
		String tjflag="false";
		try {
			SQLParam param = new SQLParam();
			param.addParam(LoginInfoUtil.getLoginUserName());
			TCategoryShopPO shopPO = dao.queryPOByKey("grxx_queryShopxxByYhm", param, TCategoryShopPO.class);
			if("true".equals(StringUtil.parse2String(res.get("flag")))){
				String htmlPath = StringUtil.parse2String(res.get("htmlPath"));
				TabRecruitPO po=(TabRecruitPO) req.getForm("form_recruit",TabRecruitPO.class);
				po.setId(new DE_ID(SwordSequenceUtils.generateRandomString()));
				po.setWebusername(new DE_WEBUSERNAME(LoginInfoUtil.getLoginUserName()));
				po.setZpLogo(new DE_ZP_LOGO(shopPO.getCBrandXtbpath().value));
				po.setZpShopname(new DE_ZP_SHOPNAME(shopPO.getCBrandTitle().value));
				po.setWebpath(new DE_WEBPATH(htmlPath));
				po.setWebdomain(new DE_WEBDOMAIN(WebRootUtil.getWebRootPath()));
				po.setZpShzt(new DE_ZP_SHZT("dsh"));
				po.setZpShsj(new DE_ZP_SHSJ(DateFormatUtil.getNowDate()));
				po.setZpFbsj(new DE_ZP_FBSJ(DateFormatUtil.getNowDate()));
				po.setZpOpptime(new DE_ZP_OPPTIME(DateFormatUtil.getNowDate()));
				if ("1".equals(po.getZpSfnew().value)) {
					shopPO.setCBrandNewnum(new DE_C_BRAND_NEWNUM(shopPO.getCBrandNewnum().value-1));
					dao.updatePO(shopPO);
					//修改招聘分类的时间
					Map map = CacheUtil.getCacheData("DM_YW_CATEGORY", "caption", "招聘");
					TCategoryPO categoryPO = new TCategoryPO();
					categoryPO.setCategorySfnew(new DE_CATEGORY_SFNEW("1"));
					categoryPO.setCategoryId(new DE_CATEGORY_ID(StringUtil.parse2String(map.get("code"))));
					categoryPO.setCategoryOpptime(new DE_CATEGORY_OPPTIME(DateFormatUtil.getNowDate()));
					dao.updatePO(categoryPO);
				}
				int i = dao.insertPO(po);
				if (i>0) {
					tjflag = "true";
					dao.commitAll();
				}
			}
		} catch (Exception e) {
			dao.rollbackAll();
			e.printStackTrace();
		}
		res.addAttr("tjflag", tjflag);
		return res;
	}

}